#include <vector>

using namespace std;

class Solution {
public:
    vector<vector<int>> merge(vector<vector<int>>& intervals) {
        sort(intervals.begin(), intervals.end());
        vector<vector<int>> ans;
        for (vector<int>& pair : intervals) {
            if (ans.empty()) {
                ans.emplace_back(pair);
                continue;
            } else {
                if (pair[0] <= ans.back()[1]) {
                    ans.back()[1] = max(ans.back()[1], pair[1]);
                } else {
                    ans.emplace_back(pair);
                }
            }
        }
        return ans;
    }
};